<template>
    <!-- 病例管理-处方历史 -->
    <div class="per-table">
        <yx-table :is-fit="false" :table-data="tableData" :columns="columns" :page-obj="pageObj" :loading="loading" :tableHeightDefault="600">
            <template slot="patientSex" slot-scope="scope">
                <div>{{scope.scope.patientSex | sexFilter}}</div>
            </template>
            <template slot="prescriptionType" slot-scope="scope">
                <div>{{scope.scope.prescriptionType | perTypeFilter}}</div>
            </template>
            <template slot="operate" slot-scope="scope">
                <div class="operate-btn">
                    <div>
                        <el-button v-if="$global.isAuto && scope.scope.loginMiniQrcodeurl && scope.scope.prescriptionState != '药师审核驳回'" style="color: #006DB8;" size="small" type="text" @click="viewCode(scope.scope.loginMiniQrcodeurl)">
                            查看二维码
                        </el-button>
                    </div>
                    <div>
                        <el-button style="color: #006DB8;" size="small" type="text" @click="pharmacyDetails(scope.scope)">
                            用药详情
                        </el-button>
                    </div>
                    <div>
                        <el-button style="color: #D96F11;" size="small" type="text" @click="viewPerscription(scope.scope)">
                            打印处方
                        </el-button>
                    </div>
                    <div>
                        <el-button style="color: #21AD49;" size="small" type="text" @click="viewPerscription(scope.scope)">
                            保存处方
                        </el-button>
                    </div>
                    <div v-if="userType == 'D' && scope.scope.prescriptionState == '药师审核驳回' && ($global.platform('lj') || (!$global.platform('lj') && scope.scope.paymentMethod != '2')) && scope.scope.userType != 'YD'">
                        <el-button style="color: #006DB8;" size="small" type="text" @click="editPerscription(scope.scope)">
                            编辑处方
                        </el-button>
                    </div>
                </div>
            </template>
        </yx-table>
        <pharmacy-details :id="parseInt(checkedData.id)" v-if="pharmacyVisible" ref="pharmacy"></pharmacy-details>

        <pdf-dialog ref="pdfDialog"></pdf-dialog>

        <!-- 中西药弹框 start -->
        <drug-dialog ref="drugDialog" @closed="closeDialog('westernMedicineDialog')"></drug-dialog>
        <!-- 中西药弹框 end -->

        <!-- ------------------ 患者二维码 ------------------ -->
        <patient-code-dialog ref="patientCodeDialog" is-onlyready></patient-code-dialog>
    </div>
</template>

<script>
    import yxTable from '../../../components/yxtable/yx-table'
    import pharmacyDetails from '../common/pharmacy-details'
    import pdfDialog from '@/components/chatroom/view-pdf';
    import DrugDialog from '@/components/chatroom/drug-dialog';
    import patientCodeDialog from '@/components/patient-qr-dialog';
    export default {
        components: {
            yxTable,
            pharmacyDetails,
            pdfDialog,
            'drug-dialog': DrugDialog,
            patientCodeDialog
        },
        props: {
            queryList: {
                type: Object,
                default () {
                    return {}
                }
            },
        },
        data() {
            return {
                pharmacyVisible: false,
                columns: [], // 操作列
                loading: false,
                pageObj: {
                    size: 10,
                    total: 0,
                    currentPage: 1,
                    func: (currentPage) => {
                        this.pageTurning(currentPage)
                    },
                    handleSizeChange: (size) => {
                        this.sizeChange(size)
                    }
                },
                tableData: [],
                checkedData: {},
                drugOrderId: null, //处方订单编号
                drugOrderMsg: {}, //处方订单信息
                drugType: '', //中西药类型
                westernMedicineDialog: false, //西药弹框
                userType: '',
            }
        },
        mounted() {
            this.userType = this.$authInfo.getAuthType()
            this.columns = [
                { label: '问诊患者', prop: 'patientName' },
                { label: '性别', slot: 'patientSex' },
                { label: '年龄', prop: 'patientAge' },
                { label: '联系电话', prop: 'patientPhone' },
                { label: '诊断', prop: this.$authInfo.getAuthType() == 'D' ? 'diagnosisDescribe' : 'diagnosis' },
                { label: '用户来源', prop: 'userSource' },
                { label: '问诊日期', prop: this.$authInfo.getAuthType() == 'D' ? 'acceptsTime' : 'inquiryTime' },
                { label: '开处方日期', prop: 'createTime' },
                { label: '审核状态', prop: 'prescriptionState' },
                { label: '处方类型', slot: 'prescriptionType' },
                { label: '操作', slot: 'operate', width: 100 },
            ];
            if (this.$authInfo.getAuthType() == 'D') {
                this.columns.splice(9, 0, { label: '驳回理由', prop: 'prescriptionReasonsInvalidation' })
            }

            this.getOpenPrescriptions()
        },
        methods: {
            //用药详情
            pharmacyDetails(e) {
                this.checkedData = e;
                this.pharmacyVisible = true
                this.$nextTick(() => {
                    this.$refs.pharmacy.init()
                })
            },
            // 打印保存处方
            viewPerscription(e) {
                let params = {
                    id: e.id,
                    resource: 4
                };

                this.$refs.pdfDialog && this.$refs.pdfDialog.onOpen(params);
            },
            //查询
            queryData() {
                this.getOpenPrescriptions()
            },
            pageTurning(currentPage) {
                this.pageObj.currentPage = currentPage;
                this.getOpenPrescriptions();
            },
            sizeChange(size) {
                this.pageObj.size = size;
                this.getOpenPrescriptions();
            },
            getOpenPrescriptions() {
                this.loading = true;
                let params = {
                    current: this.pageObj.currentPage,
                    size: this.pageObj.size,
                    endDate: this.queryList.time[1] || '',
                    startDate: this.queryList.time[0] || '',
                    patientName: this.queryList.patientName,
                    patientPhone: this.queryList.patientPhone,
                    prescriptionState: this.queryList.prescriptionState,
                    prescriptionType: this.queryList.prescriptionType,
                    userType: this.queryList.userType
                }

                let apiName = this.$authInfo.getAuthType() == 'D' ? 'getOpenPrescriptions' : 'getPatientPrescriptionHistoryList';
                if (apiName == 'getPatientPrescriptionHistoryList') {
                    params.maxTime = this.queryList.time[1] || '';
                    params.minTime = this.queryList.time[0] || '';
                };
                this.$ajax[apiName](params).then(res => {
                    this.loading = false;
                    let { code, data } = res;
                    if (code === 1) {
                        this.tableData = data.list
                        this.pageObj.total = data.total
                    }
                }).catch(err => {
                    this.loading = false;
                });
            },
            editPerscription(val) { //编辑处方
                this.drugOrderId = val.id

                let data = {
                    drugId: Number(val.id),
                }

                this.$refs.drugDialog.onOpen({
                    data,
                    isEdit: true,
                    paymentMethod: val.paymentMethod
                })

            },
            closeDialog(dialogName) { //关闭弹框
                if (dialogName == 'westernMedicineDialog') { //西药弹框
                    this.westernMedicineDialog = false;
                    this.getOpenPrescriptions()
                }
            },
            // 查看二维码
            viewCode(url) {
                let data = {
                    loginMiniQrcodeurl: url
                };

                this.$refs.patientCodeDialog && this.$refs.patientCodeDialog.onOpen({ data })
            }
        }
    }
</script>

<style lang="scss">
    .per-table {
        /deep/ .el-button--small {
            padding: 5px 15px;
        }
    }
</style>